/**
 * 
 */
package com.yt.xiao.code.generate.run;

import java.util.ArrayList;
import java.util.List;

import com.yt.xiao.code.generate.dal.po.Table;
import com.yt.xiao.code.generate.maker.FMMakerImpl;
import com.yt.xiao.code.generate.processor.DBProcessor;
import com.yt.xiao.code.generate.processor.MySQLProcessorImpl;

/**
 * Hello world!
 * 
 */
public class XYCodeGeneratorStarter {

    public static void main(String[] args) throws Exception {
        
        new XYCodeGeneratorStarter().run();
    }

    public void run() throws Exception {
        java.util.Properties prop = new java.util.Properties();
        prop.load(this.getClass().getClassLoader().getResourceAsStream("xycg-config.properties"));
        String driverClass = prop.getProperty("db.driver.class");
        String jdbcUrl = prop.getProperty("db.jdbc.url");
        String user = prop.getProperty("db.user");
        String password = prop.getProperty("db.password");
        DBProcessor mp = new MySQLProcessorImpl(driverClass, jdbcUrl, user, password);
        mp.setTablePrefix(prop.getProperty("cg.table.prefix"));
        mp.setTableNameSwitch("true".equalsIgnoreCase(prop.getProperty("cg.tables.include", "true")));

        String tableNames = prop.getProperty("cg.tables");
        if (tableNames != null && tableNames.trim().length() > 0) {
            String[] tna = tableNames.split(",");
            List<String> tnlist = new ArrayList<String>();
            for (int i = 0; i < tna.length; i++) {
                if (tna[i].trim().length() > 0) {
                    tnlist.add(tna[i].trim());
                }
            }
            if (tnlist.size() > 0) {
                mp.setTableNameList(tnlist);
            }
        }

        List<Table> tableList = mp.loadTableList();
        // System.out.println(tableList);
        FMMakerImpl maker = new FMMakerImpl();
        maker.setEncoding("utf-8");
        maker.setJdbcInfo(mp.getJdbcInfo());
        maker.setLtdAbbr(prop.getProperty("cg.ltd.abbr", "ltd"));
        maker.setModuleName(prop.getProperty("cg.module.name", "abc"));
        maker.setOutPath(prop.getProperty("cg.output.path", "out"));
        maker.setTemplatePath(prop.getProperty("cg.template.path", "tpl"));
        maker.setTableList(tableList);
        maker.setActionSuffix(prop.getProperty("uri.action.name", "action"));
        maker.make();
        System.out.println("generate successfully!");
    }

}
